﻿using System;
using System.Windows;
using System.Windows.Input;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Tfs.OnTheRoad.Model;
using Tfs.OnTheRoad.Pages;
using Tfs.OnTheRoad.ViewModel;
namespace Tfs_OnTheRoad
{
    /// <summary>
    /// WorkItems Overview Page
    /// </summary>
    public partial class WorkItemsOverviewPage : BasePage
    {
        public WorkItemsOverviewPage()
        {
            InitializeComponent();
            this.Loaded += delegate(object sender, RoutedEventArgs eventArgs) { 
            CreateNewButton("Chart", "/Images/appbar.chart.rest.png",
                                        delegate(object s, EventArgs e)
                                        {
                                            if (ChartContainer.Visibility == System.Windows.Visibility.Collapsed)
                                            {
                                                ViewModel.CalculateSummary();
                                                ChartContainer.Visibility = System.Windows.Visibility.Visible;
                                            }
                                            else
                                            {
                                                ChartContainer.Visibility = System.Windows.Visibility.Collapsed;
                                            }
                                        });
            };              
        }

        WorkItemsOverviewViewModel ViewModel;

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (this.DataContext == null)
            {
                string[] wiTypes = null;
                string[] states = null;
                Query currentQuery = null;
                if (this.NavigationContext.QueryString.ContainsKey("type"))
                    wiTypes = new string[] { this.NavigationContext.QueryString["type"] };

                if (this.NavigationContext.QueryString.ContainsKey("state"))
                    states = new string[] { this.NavigationContext.QueryString["state"] };

                if(PhoneApplicationService.Current.State.ContainsKey("CurrentQuery"))
                    currentQuery = PhoneApplicationService.Current.State["CurrentQuery"] as Query;
                
                if (wiTypes == null && 
                    PhoneApplicationService.Current.State.ContainsKey("WorkItemsTypes"))
                {
                    wiTypes = PhoneApplicationService.Current.State["WorkItemsTypes"] as string[];                    
                }
                
                ViewModel = new WorkItemsOverviewViewModel(currentQuery, wiTypes, states);
                this.DataContext = ViewModel;
                ChartContainer.DataContext = ViewModel.WorkItemsChartViewModel;
            }
        }

        private void Pivot_LoadingPivotItem(object sender, PivotItemEventArgs e)
        {
            var wiViewModel = e.Item.DataContext as WorkItemListViewModel;
            if (wiViewModel != null)
            {
                if (!wiViewModel.Loaded)
                    wiViewModel.LoadData();
            }            
        }

        private void ChartContainer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ChartContainer.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}
